const Pay = require('../../utils/mp-pay')
/**
 * 商户自行实现依据订单号out_order_no查询订单状态方法，
 */
function queryOrderStatus(data) {
	let {
		out_order_no
	} = data
	return new Promise(function(resolve, reject) {
		// 依据out_order_no查询订单状态，返回结果
		// 0：支付成功 1：支付超时 2：支付失败 3：支付关闭 4：取消支付 9：订单状态未知
		resolve({
			code: 9
		})
	})
}
Page({
	
	/**
	 * 确认支付
	 */
	onMerchantPay: function() {
		tt.showLoading({
			title: '订单提交中...'
		})

		// 请求后端接口进行下单，并获取拉起小程序收银台的签名订单信息
		// 可以使用Server SDK生成拉起小程序收银台的参数
		tt.request({
			url: `http://192.168.0.143:3000/createOrder`, // 替换为后端真实下单地址
			method: 'POST',
			data: {
				alipayUrl: 'alipay_sdk=alipay-sdk-java-3.4.27.ALL&app_id=2021001157697271&biz_content=%7B%22body%22%3A%22%E6%94%AF%E4%BB%98%22%2C%22out_trade_no%22%3A%2258935214283701%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22subject%22%3A%22%E6%94%AF%E4%BB%98%22%2C%22timeout_express%22%3A%2230m%22%2C%22total_amount%22%3A%220.01%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Fonline.decentcn.cn%2Fpay%2FailPayBack&sign=TPWsx0egH4ncUr%2BMMau7DqwyguUTpJ9hjlHeBNFRk4t9R%2FQlQ%2BcvNqzxI4rZIdEfr8BiZuBPK5uiHTpFVN9kVxZidq8pR5P%2B0WcCokYFwXGwYBb3kumX5Ht7xb6XYJ%2F18jqEmebOpxlXcpmWV9N1cZEOLN7J8DYp%2BgRVcumVIDTtXViUn9OouCQLjFR01%2FMcxz1p3vzAWNjzASfLWz25whTzu84%2BzjHwk5en%2FsVnwka0o0XscpBRbOFjTYLe3TcUL5Bqxags9roORAA60bm59QZ6Ppp%2FSODnZsUiZ8HBH%2FNUjZq8Gw9zwl4wFMuinrv27ZHGKi7TfCcAgaxg9zfZZQ%3D%3D&sign_type=RSA2&timestamp=2020-05-13+14%3A42%3A22&version=1.0',
				// wxUrl:'https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx19124344335292c0c886edc01971959000&package=2612935109',
				outOrderNo: 'MicroApp' + Math.floor(Math.random() * 10000000000), // 商户侧订单号
				timestamp: new Date().getTime(), // 时间戳，秒
				totalAmount: 1, // 金额，单位为分
				uid: '2019012211', // 用户标识
				subject: 'microapp test', //商户订单名称
				body: 'microapp test', //商户订单详情
				tradeTime: Math.floor(new Date().getTime() / 1000) + '',
				notifyUrl: `http://online.decentcn.cn/pay/ailPayBack`,
				validTime: 36000000,
				type: 1,
			},
			success(res) {
				tt.hideLoading()
				/**
				 * res 数据结构格式：
				 * {
				 *	'1.0': '{"app_id":"80000...',
				 *	'2.0': '{"alipay_url":"alip...'
				 * }
				 */
				console.log("res", res.data)
				Pay(res.data, queryOrderStatus)
					.then(res => {
						console.log('ok', res)
					})
					.catch(err => {
						console.log('fail', err)
					})
			}
		})
	}
})
